﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;
using DaInterFace.PDA.P02;

namespace P02
{
    /// <summary>
    /// A08 执行采样单结束操作
    /// </summary>
    public class A12 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P02A12P P = (P02A12P)handler.XML.Deserialize(typeof(P02A12P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@SampId", SqlDbType.NVarChar, 50);
                sps[0].Value = P.SampId;

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @IsOver BIT\r\n");
                sql.Append("DECLARE @IsUse BIT\r\n");
                sql.Append("SELECT @IsOver=FCheckBox,@IsUse=FCheckBox1 FROM KEW_PurchSampling WHERE FID=@SampId\r\n");
                sql.Append("IF IsNull(@IsOver,0) = 0 BEGIN \r\n");
                sql.Append("SELECT 'FALSE','该采样单正在使用，不能启动' \r\n");
                sql.Append("RETURN \r\nEND\r\n");
                sql.Append("IF IsNull(@IsUse,0) = 1 BEGIN \r\n");
                sql.Append("SELECT 'FALSE','已经化验的采样单不能重新启动' \r\n");
                sql.Append("RETURN \r\nEND\r\n");

                sql.Append("SELECT 'TRUE','启动成功' \r\n");
                sql.Append("UPDATE KEW_PurchSampling SET FCheckBox=0 WHERE FID=@SampId");

                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);

                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "TRUE")
                    {
                        CR.Result = EnumResult.Normal;
                        CR.Message = String.Format("操作员{0}对账套{1}中的取样单进行了启动操作{2}", user.UserName, P.BaseNo, P.SampId);
                    }
                    else
                    {
                        CR.Result = EnumResult.Normal;
                        CR.Message = rs.Data.Tables[0].Rows[0][1].ToString(); ;
                    }
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "数据库错误";
                }
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = String.Format("发生错误:{0}", ex.Message);
            }
        }
    }


}
